home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / onetime.zip / FFCR.C < prev    next >
C/C++ Source or Header  |  1995-04-26  |  2KB  |  76 lines

  1. /*************************************************************************/
  2. /*          CREATES RANDOM CODE.KEY FILE FOR BINARY FILE ENCODING        */
  3. /*                   range of random numbers 0 - 255                     */
  4. /*************************************************************************/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <io.h>
  10.  
  11. #define INVOCATION_ERROR 5
  12. #define FILE_ERR 10
  13. #define ARGCOUNT 2
  14. #define FILENAME argv[1]
  15. #define BUFFERSIZE 8192
  16. #define RANGE 255
  17. // ***        ^^^ range of random "shifts" ***
  18.  
  19. int create_file( char *filename, long int count );
  20.  
  21. void main( int argc, char **argv )
  22. {
  23.    long len;
  24.    FILE *f;
  25.  
  26.      if( argc != ARGCOUNT )
  27.         {
  28.         puts(
  29.         "Form: ffcr FILE_NAME, where FILE_NAME is the file to be encoded."
  30.         );
  31.  
  32.         exit ( INVOCATION_ERROR );
  33.         }
  34.  
  35.      f = fopen( FILENAME, "r" );
  36.  
  37.  
  38.      len = filelength( fileno( f ) );
  39.      printf( "\n\nCreating a CODE.KEY file at least %ld bytes long.\n", len );
  40.      create_file( "code.key", len );
  41.  
  42.      fclose ( f );
  43.  
  44.  
  45. }
  46.  
  47. int create_file( char *filename, long int count )
  48. {
  49.    register int r;
  50.    long n;
  51.    FILE *fp;
  52.  
  53.      randomize();
  54.  
  55.      if( NULL == ( fp = fopen( filename, "w" ) ) )
  56.        {
  57.        printf( "\nCannot open file %s\n", filename );         
  58.        exit ( FILE_ERR );
  59.        }
  60.  
  61.  
  62.       if( setvbuf( fp, NULL, _IOFBF, BUFFERSIZE ) )
  63.          exit( FILE_ERR );
  64.  
  65.      for( n = 0; n < count; n++ )
  66.         {
  67.         r = random( RANGE );
  68.         fputc( r, fp );
  69.         }
  70.  
  71.      fclose( fp );
  72.  
  73.      return ( n );
  74.  
  75. }
  76.